home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 June / PCpro_2005_06.ISO / files / opensource / amc / amc_install.exe / {app} / Scripts / IMDB.ifs < prev    next >
Encoding:
Text File  |  2005-03-01  |  17.4 KB  |  506 lines

  1. (***************************************************
  2.  
  3. Ant Movie Catalog importation script
  4. www.antp.be/software/moviecatalog/
  5.  
  6. [Infos]
  7. Authors=Antoine Potten
  8. Title=IMDB
  9. Description=Import data & picture from IMDB (optional image from Amazon)
  10. Site=us.imdb.com
  11. Language=EN
  12. Version=1.0
  13. Requires=3.5.0
  14. Comments=Based on the script made for version 3.x by Antoine Potten, Danny Falkov, Kai Blankenhorn, lboregard, Ork, Trekkie, Youri Heijnen
  15. License=This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  16. GetInfo=1
  17.  
  18. [Options]
  19. ImageKind=0|1|0=No image|1=IMDB small image, from the main movie page|2=IMDB large image if found easily, else small image|3=First search for Amazon large image, then IMDB large one, then IMDB small image if other failed|4=First search for Amazon large image, then directly take IMDB small image if the first one failed|5=IMDB large image if found easily, else search for Amazon large image, then take IMDB small image if others failed
  20. BatchMode=1|0|0=Normal working mode, prompts user when needed|1=Does not display any window, takes the first movie found|2=Same as 1, but it uses the URL field if available to update movie information
  21. PopularSearches=1|1|0=Do not use the popular searches page, directly show full search results|1=Show popular searches first, I'll click on "Find more" if needed
  22. ActorsLayout=0|0|0=Only actor names, separated by commas|1=Only actor names, separated by linebreaks|2=Actors names with character names between parenthesis separated by commas|3=Actors names with character names between parenthesis separated by linebreaks|4=Actor names like on IMDB page, with "...." and separated by linebreaks
  23. MultipleValuesCountry=0|0|0=Only take first value for Country|1=Take full list, separated by commas|2=Take full list, separated by slashes
  24. MultipleValuesCategory=0|0|0=Only take first value for Category|1=Take full list, separated by commas|2=Take full list, separated by slashes
  25. MultipleValuesLanguages=0|0|0=Only take first value for Languages|1=Take full list, separated by commas|2=Take full list, separated by slashes
  26. DescriptionSelection=1|1|0=Take the short summary, from main page (faster)|1=Show a list of available summaries|2=Take the longest summary
  27. GetTagline=0|0|0=Do not get tagline|1=Put it in Description field, before the summary|2=Put it in the Comment field, before the comments
  28.  
  29. ***************************************************)
  30.  
  31. program IMDB;
  32.  
  33. uses
  34.   StringUtils1;
  35.  
  36. var
  37.   MovieName: string;
  38.   MovieURL: string;
  39.   MovieNumber: string;
  40.  
  41. // ***** analyzes the results page that asks to select a movie from a list *****
  42.  
  43. procedure AnalyzeResultsPage(Address: string);
  44. var
  45.   PageText: string;
  46.   Value: string;
  47. begin
  48.   PageText := GetPage(Address);
  49.   if pos('<title>IMDb', PageText) = 0 then
  50.   begin
  51.     AnalyzeMoviePage(PageText)
  52.   end else
  53.   begin
  54.     if Pos('<b>No Matches.</b>', PageText) > 0 then
  55.     begin
  56.       if GetOption('BatchMode') = 0 then
  57.         ShowMessage('No movie found for this search');
  58.       Exit;
  59.     end;
  60.     if GetOption('BatchMode') = 0 then
  61.     begin
  62.       PickTreeClear;
  63.       repeat
  64.         Value := TextBefore(PageText, '<ol>', '<b>');
  65.         if Value <> '' then
  66.         begin
  67.           HTMLRemoveTags(Value);
  68.           HTMLDecode(Value);
  69.           PickTreeAdd(Value, '');
  70.         end;
  71.         Value := TextBetween(PageText, '<ol>', '</ol>');
  72.         PageText := RemainingText;
  73.       until not AddMovieTitles(Value);
  74.       Value := TextBefore(PageText, '"><b>more titles</b></a>', '<a href="');
  75.       if Value <> '' then
  76.         PickTreeMoreLink('http://us.imdb.com' + Value);
  77.       if PickTreeExec(Address) then
  78.         AnalyzeResultsPage(Address);
  79.     end
  80.     else
  81.     begin
  82.       Value := TextBetween(TextBetween(PageText, '<ol>', '</ol>'), '<li>', '</li>');
  83.       if Value <> '' then
  84.         AnalyzeResultsPage(TextBetween(Value, '<a href="', '">'));
  85.     end;
  86.   end;
  87. end;
  88.  
  89. // ***** adds the titles contained in <ol>'s items *****
  90.  
  91. function AddMovieTitles(List: string): Boolean;
  92. var
  93.   Value: string;
  94.   Address: string;
  95. begin
  96.   Result := False;
  97.   Value := TextBetween(List, '<li>', '</li>');
  98.   List := RemainingText;
  99.   while Value <> '' do
  100.   begin
  101.     Address := TextBetween(Value, '<a href="', '">');
  102.     HTMLRemoveTags(Value);
  103.     HTMLDecode(Value);
  104.     PickTreeAdd(Value, 'http://us.imdb.com' + Address);
  105.     Result := True;
  106.     Value := TextBetween(List, '<li>', '</li>');
  107.     List := RemainingText;
  108.   end;
  109. end;
  110.  
  111. // ***** analyzes the page containing movie information *****
  112.  
  113. procedure AnalyzeMoviePage(PageText: string);
  114. var
  115.   Value, Value2, Value3, FullValue: string;
  116. begin
  117.   MovieNumber := TextBetween(PageText, '<input type="hidden" name="arg" value="', '"><input');
  118.   MovieURL := 'http://us.imdb.com/title/tt' + MovieNumber;
  119.   // URL
  120.   if CanSetField(fieldURL) then
  121.     SetField(fieldURL, MovieURL);
  122.   // Original Title & Year
  123.   if CanSetField(fieldOriginalTitle) or CanSetField(fieldYear) then
  124.   begin
  125.     Value := TextBetween(PageText, '<title>', '</title>');
  126.     Value2 := TextBefore(Value, ' (', '');
  127.     Value := RemainingText;
  128.     HTMLDecode(Value2);
  129.     if CanSetField(fieldOriginalTitle) then
  130.       SetField(fieldOriginalTitle, Value2);
  131.     if Pos('/', Value) > 0 then
  132.       Value2 := TextBefore(Value, '/', '')
  133.     else
  134.       Value2 := TextBefore(Value, ')', '');
  135.     if CanSetField(fieldYear) then
  136.       SetField(fieldYear, Value2);
  137.   end;
  138.   // Rating
  139.   if CanSetField(fieldRating) then
  140.   begin
  141.     Value := TextBetween(PageText, '/rating-stars/', '/rating-vote/');
  142.     SetField(fieldRating, TextBetween(Value, '<b>', '/'));
  143.   end;
  144.   // Picture
  145.   if CanSetPicture then
  146.   begin
  147.     case GetOption('ImageKind') of
  148.       1:  ImportSmallPicture(PageText);
  149.       2:  if not ImportLargePicture('http://us.imdb.com/gallery/ss/' + MovieNumber) then
  150.             ImportSmallPicture(PageText);
  151.       3:  if not ImportAmazonPicture(PageText) then
  152.             if not ImportLargePicture('http://us.imdb.com/gallery/ss/' + MovieNumber) then
  153.               ImportSmallPicture(PageText);
  154.       4:  if not ImportAmazonPicture(PageText) then
  155.             ImportSmallPicture(PageText);
  156.       5:  if not ImportLargePicture('http://us.imdb.com/gallery/ss/' + MovieNumber) then
  157.             if not ImportAmazonPicture(PageText) then
  158.               ImportSmallPicture(PageText);
  159.     end;
  160.   end;
  161.   // Director
  162.   if CanSetField(fieldDirector) then
  163.   begin
  164.     Value := TextBetween(PageText, '<b class="blackcatheader">Directed by</b><br>', '<br>' + #13);
  165.     Value := StringReplace(TextAfter(Value, '">'), '<br>', ', ');
  166.     HTMLRemoveTags(Value);
  167.     HTMLDecode(Value);
  168.     SetField(fieldDirector, Value);
  169.   end;
  170.   // Actors
  171.   if CanSetField(fieldActors) then
  172.   begin
  173.     Value := TextBetween(PageText, 'ast overview', '</div>');
  174.     if Value = '' then
  175.       Value := TextBetween(PageText, 'redited cast', '</div>');
  176.     if Value <> '' then
  177.     begin
  178.       Value := TextAfter(Value, '</tr> ');
  179.       FullValue := '';
  180.       case GetOption('ActorsLayout') of
  181.         0, 1:
  182.           while Pos('<tr>', Value) > 0 do
  183.           begin
  184.             Value2 := TextBetween(Value, '<tr>', '</tr>');
  185.             Value := RemainingText;
  186.             if Pos('<a href="fullcredits">(more)</a>', Value2) > 0 then
  187.               Break;
  188.             if FullValue <> '' then
  189.               FullValue := FullValue + #13#10;
  190.             FullValue := FullValue + TextBefore(Value2, '</td>', '');
  191.           end;
  192.         2, 3:
  193.           while Pos('<tr>', Value) > 0 do
  194.           begin
  195.             Value2 := TextBetween(Value, '<tr>', '</tr>');
  196.             Value := RemainingText;
  197.             if Pos('<a href="fullcredits">(more)</a>', Value2) > 0 then
  198.               Break;
  199.             if FullValue <> '' then
  200.               FullValue := FullValue + #13#10;
  201.             FullValue := FullValue + TextBefore(Value2, '</td>', '');
  202.             Value2 := TextBetween(RemainingText, '<td valign="top">', '</td>');
  203.             if Value2 <> '' then
  204.               FullValue := FullValue + ' (as ' + Value2 + ')';
  205.           end;
  206.         4:
  207.           begin
  208.             FullValue := TextBefore(Value, '</tr><tr><td colspan="2">', '');
  209.             if FullValue = '' then
  210.               FullValue := Value;
  211.             FullValue := StringReplace(FullValue, '</tr>', #13#10);
  212.           end;
  213.       end;
  214.       HTMLRemoveTags(FullValue);
  215.       HTMLDecode(FullValue);
  216.       case GetOption('ActorsLayout') of
  217.         0, 2:
  218.           FullValue := StringReplace(FullValue, #13#10, ', ');
  219.       end;
  220.       SetField(fieldActors, FullValue);
  221.     end;
  222.   end;
  223.   //Country
  224.   if CanSetField(fieldCountry) then
  225.   begin
  226.     SetField(fieldCountry, ImportList(PageText, GetOption('MultipleValuesCountry'), '/Countries/'));
  227.   end;
  228.   //Category
  229.   if CanSetField(fieldCategory) then
  230.   begin
  231.     SetField(fieldCategory, ImportList(PageText, GetOption('MultipleValuesCategory'), '/Genres/'));
  232.   end;
  233.   // Language
  234.   if CanSetField(fieldLanguages) then
  235.   begin
  236.     SetField(fieldLanguages, ImportList(PageText, GetOption('MultipleValuesLanguages'), '/Languages/'));
  237.   end;
  238.   //Description
  239.   if CanSetField(fieldDescription) then
  240.   begin
  241.     Value := TextBetween(PageText, '<b class="ch">Plot Outline:</b>', '<br><br>');
  242.     if Value = '' then
  243.       Value := TextBetween(PageText, '<b class="ch">Plot Summary:</b>', '<br><br>');
  244.     if Value <> '' then
  245.       SetField(fieldDescription, ImportSummary(Value));
  246.   end;
  247.   // Comments
  248.   if CanSetField(fieldComments) then
  249.   begin
  250.     Value := TextAfter(PageText, '/comments">');
  251.     if Value <> '' then
  252.     begin
  253.       Value := TextBetween(Value, '<p>', '</p>');
  254.       Value := StringReplace(Value, #13#10, ' ');
  255.       Value := StringReplace(Value, '<br>', #13#10);
  256.       HTMLRemoveTags(Value);
  257.       HTMLDecode(Value);
  258.       Value := Trim(Value);
  259.       while Pos('  ', Value) > 0 do
  260.         Value := StringReplace(Value, '  ', ' ');
  261.       while Pos(#13#10, Value) = 1 do
  262.         Delete(Value, 1, 2);
  263.       SetField(fieldComments, Value);
  264.     end;
  265.   end;
  266.   // Length
  267.   if CanSetField(fieldLength) then
  268.   begin
  269.     Value := TextBetween(PageText, '<b class="ch">Runtime:</b>' + #13#10, ' ');
  270.     if Value <> '' then
  271.     begin
  272.       if Pos(':', Value) > 0 then
  273.         SetField(fieldLength, TextAfter(Value, ':'))
  274.       else
  275.         SetField(fieldLength, Value);
  276.     end;
  277.   end;
  278.   // TagLine
  279.   if GetOption('GetTagline') > 0 then
  280.   begin
  281.     Value := TextBetween(PageText, 'Tagline:</b>', #13);
  282.     if Pos('<a', Value) > 0 then
  283.       Value := TextBefore(Value, '<a', '');
  284.     HTMLRemoveTags(Value);
  285.     HTMLDecode(Value);
  286.     Value := Trim(Value);
  287.     if Value <> '' then
  288.     begin
  289.       Value := '"' + Value + '"';
  290.       case GetOption('GetTagline') of
  291.         1:
  292.           if CanSetField(fieldDescription) then
  293.             SetField(fieldDescription, Value + #13#10 + GetField(fieldDescription));
  294.         2:
  295.           if CanSetField(fieldComments) then
  296.             SetField(fieldComments, Value + #13#10 + GetField(fieldComments));
  297.       end;
  298.     end;
  299.   end;
  300. end;
  301.  
  302. // ***** Imports lists like Genre, Country, etc. depending of the selected option *****
  303.  
  304. function ImportList(PageText: string; MultipleValues: Integer; StartTag: string): string;
  305. var
  306.   Value, Value2: string;
  307. begin
  308.   if MultipleValues = 0 then
  309.   begin
  310.     Value := TextBetween(PageText, StartTag, '</a>');
  311.     Value2 := TextAfter(Value, '">');
  312.   end
  313.   else
  314.   begin
  315.     Value := TextBetween(PageText, StartTag, #13#10);
  316.     Value2 := TextBefore(Value, ' <a href="/rg', '');
  317.     if Value2 <> '' then
  318.       Value := Value2;
  319.     Value2 := TextAfter(Value, '">');
  320.     HTMLRemoveTags(Value2);
  321.     if MultipleValues = 1 then
  322.       Value2 := StringReplace(Value2, ' / ', ', ');
  323.   end;
  324.   HTMLDecode(Value2);
  325.   Result := Value2;
  326. end;
  327.  
  328. // ***** functions to import the different pictures kinds, depending of the option selected by user *****
  329.  
  330. function ImportSmallPicture(PageText: string): Boolean;
  331. var
  332.   Value: string;
  333. begin
  334.   Result := False;
  335.   Value := TextBetween(PageText, '<img border="0" alt="cover" src="', '"');
  336.   if Value <> '' then
  337.   begin
  338.     GetPicture(Value);
  339.     Result := True;
  340.   end;
  341. end;
  342.  
  343. function ImportLargePicture(Address: string): Boolean;
  344. var
  345.   Value, Value2: string;
  346. begin
  347.   Result := True;
  348.   Value := GetPage(Address);
  349.   if SearchForLargePicture(Value, 'Onesheet_text', False) then
  350.     Exit;
  351.   if SearchForLargePicture(Value, 'keyart01', True) then
  352.     Exit;
  353.   if SearchForLargePicture(Value, 'keyart02', True) then
  354.     Exit;
  355.   if SearchForLargePicture(Value, 'oster', True) then // poster, usposter, Poster
  356.     Exit;
  357.   if SearchForLargePicture(Value, 'pos01', True) then
  358.     Exit;
  359.   if SearchForLargePicture(Value, 'KeyArt', True) then
  360.     Exit;
  361.   if SearchForLargePicture(Value, 'heet', True) then // Sheet & Onesheet
  362.     Exit;
  363.   if SearchForLargePicture(Value, 'OneSheetv2', True) then
  364.     Exit;
  365.   if SearchForLargePicture(Value, 'artwork', True) then
  366.     Exit;
  367.   if SearchForLargePicture(Value, 'text', True) then
  368.     Exit;
  369.   Address := TextBetween(Value, 'There are ' + #13#10 + '<a href="', '">');
  370.   if Address <> '' then
  371.     Result := ImportLargePicture('http://us.imdb.com' + Address)
  372.   else
  373.     Result := False;
  374. end;
  375.  
  376. function SearchForLargePicture(PageText: string; Name: string; PartialName: Boolean): Boolean;
  377. var
  378.   Value: string;
  379. begin
  380.   Result := False;
  381.   if PartialName then
  382.   begin
  383.     Value := TextBefore(PageText, Name + '.jpg', '/');
  384.     if Value = '' then
  385.       Exit
  386.     else
  387.       Name := Value + Name;
  388.   end;
  389.   Value := TextBefore(PageText, 'th-' + Name + '.jpg', 'src="');
  390.   if Value <> '' then
  391.   begin
  392.     GetPicture(Value + Name + '.jpg');
  393.     Result := True;
  394.   end;
  395. end;
  396.  
  397. function ImportAmazonPicture(PageText: string): Boolean;
  398. var
  399.   Value, Value2: string;
  400. begin
  401.   Result := False;
  402.   Value := TextBefore(PageText, '" title="DVD available', '<a href="');
  403.   if Value = '' then
  404.     Exit;
  405.   PageText := GetPage('http://us.imdb.com' + Value);
  406.   if Pos('unable to find exact matches', PageText) > 0 then
  407.     Exit;
  408.   if Pos('You may also be interested in these items...', PageText) > 0 then
  409.     PageText := TextBefore(PageText, 'You may also be interested in these items...', '');
  410.   Value := TextBefore(PageText, 'TZZZZZZZ.jpg', '<img src="');
  411.   if Value = '' then
  412.     Value := TextBefore(PageText, 'THUMBZZZ.jpg', '<img src="');
  413.   if Value <> '' then
  414.   begin
  415.     GetPicture(Value + 'LZZZZZZZ.jpg');
  416.     Result := True;
  417.   end;
  418. end;
  419.  
  420. // ***** Gets summaries for the movie, based on the plot outline given in parameter (that contains the URL to more summaries) *****
  421.  
  422. function ImportSummary(PlotText: string): string;
  423. var
  424.   Address, Value, Value2, PageText, Longest: string;
  425. begin
  426.   Address := TextBetween(PlotText, '<a href="/rg/title-tease/plotsummary', '">(more)</a>');
  427.   if (Address = '') or (GetOption('DescriptionSelection') = 0) then
  428.   begin
  429.     Result := Trim(TextBefore(PlotText, '<a href="/rg', ''));
  430.     if Result = '' then
  431.       Result := Trim(PlotText);
  432.     HTMLRemoveTags(Result);
  433.     HTMLDecode(Result);
  434.   end
  435.   else
  436.   begin
  437.     PageText := GetPage('http://us.imdb.com/rg/title-tease/plotsummary' + Address);
  438.     PickListClear;
  439.     Longest := '';
  440.     Value := TextBetween(PageText, '<p class="plotpar">', '</p>');
  441.     PageText := RemainingText;
  442.     while Value <> '' do
  443.     begin
  444.       Value := StringReplace(Value, #13#10, ' ');
  445.       Value := StringReplace(Value, '<br>', #13#10);
  446.       HTMLRemoveTags(Value);
  447.       HTMLDecode(Value);
  448.       while Pos('  ', Value) > 0 do
  449.         Value := StringReplace(Value, '  ', ' ');
  450.       if Length(Value) > Length(Longest) then
  451.         Longest := Value;
  452.       PickListAdd(Trim(Value));
  453.       Value := TextBetween(PageText, '<p class="plotpar">', '</p>');
  454.       PageText := RemainingText;
  455.     end;
  456.     if (GetOption('BatchMode') > 0) or (GetOption('DescriptionSelection') = 2) then
  457.       Result := Longest
  458.     else
  459.     begin
  460.       if not PickListExec('Select a description for "' + GetField(fieldOriginalTitle) + '"', Result) then
  461.         Result := '';
  462.     end;
  463.   end;
  464. end;
  465.  
  466. // ***** beginning of the program *****
  467.  
  468. begin
  469.   if CheckVersion(3,5,0) then
  470.   begin
  471.     MovieName := '';
  472.     if GetOption('BatchMode') = 2 then
  473.     begin
  474.       MovieName := GetField(fieldURL);
  475.       if Pos('imdb.com', MovieName) = 0 then
  476.         MovieName := '';
  477.     end;
  478.     if MovieName = '' then
  479.       MovieName := GetField(fieldOriginalTitle);
  480.     if MovieName = '' then
  481.       MovieName := GetField(fieldTranslatedTitle);
  482.     if GetOption('BatchMode') = 0 then
  483.     begin
  484.       if not Input('IMDB Import', 'Enter the title or the IMDB URL of the movie:', MovieName) then
  485.         Exit;
  486.     end
  487.     else
  488.       Sleep(500);
  489.     if MovieName <> '' then
  490.     begin
  491.       if Pos('imdb.com', MovieName) > 0 then
  492.         AnalyzeResultsPage(MovieName)
  493.       else
  494.       begin
  495.         MovieName := StringReplace(MovieName, '&', 'and');
  496.         if (GetOption('BatchMode') > 0) or (GetOption('PopularSearches') = 1) then
  497.           AnalyzeResultsPage('http://us.imdb.com/find?tt=1;q=' + UrlEncode(MovieName))
  498.         else
  499.           AnalyzeResultsPage('http://us.imdb.com/find?more=tt;q=' + UrlEncode(MovieName));
  500.       end;
  501.     end;
  502.   end
  503.   else
  504.     ShowMessage('This script requires a newer version of Ant Movie Catalog (at least the version 3.5.0)');
  505. end.
  506.